package com.artifact.mit.mypoint.service.async;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.widget.ImageView;

import com.artifact.mit.mypoint.service.model.SetImage;
import com.artifact.mit.mypoint.service.util.ImageUtil;

public class ImageLoader extends AsyncTask<SetImage, Boolean, Bitmap> {
	private ImageView imageView;

	@Override
	protected Bitmap doInBackground(SetImage... params) {
		try {
			URL url = new URL(params[0].imageUrl);
			imageView = params[0].imageView;
			Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
			if (params[0].circular)
				return ImageUtil.getCircularBitmap(bmp);
			return bmp;
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	protected void onPostExecute(Bitmap result) {
		imageView.setImageBitmap(result);
	}
}
